<?php


namespace App\Models\Tool;


use App\Helpers\commonFuc;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

/**创建添加控制器类
 * Class MakeAddHtml
 * @package App\Models\Tool
 */
class MakeRoutes
{

    static function make($roures,$controller_path,$xxxController,$conname){

        $controller_path='\App\\'.str_replace('/','\\',$controller_path).$xxxController.'::class';
        $xxxController = explode('Controller', $xxxController)[0];
        $xxxController = strtolower($xxxController);
        $makecontent = [];
        $start= '//@'.$xxxController.'--start';
        $makecontent[] =$start .'---'. $conname.' '.date('Y-m-d H:i:s');


        foreach ($roures as $roure){
            $makecontent[]=" Route::any('/{$xxxController}/{$roure}', [{$controller_path},'{$roure}']);";
        }
        $end = '//@'.$xxxController.'--end';
        $makecontent[] =$end;
        $makecontent[] = '//@@tool_make_routes_admin@@';
        $html_file=app_path().'/../routes/web.php';
//dd($html_file);
        $roure_old=File::get($html_file, $lock = false);
        //删除之前的生成路由
        $roure_old_this=commonFuc::getStringBetween($start,$end,$roure_old);
        $roure_old = str_replace($start.$roure_old_this.$end, '', $roure_old);
        $roure_old = explode('//@@tool_make_routes_admin@@', $roure_old);
        array_unshift($makecontent, $roure_old[0]);
        if(count($roure_old)>1) $makecontent[] = $roure_old[1];
        // 写入文件，存在的话覆盖写入
        File::replace(($html_file), join("\n",$makecontent));



    }

    /**加入权限菜单
     * @param $roures
     * @param $controller_path
     * @param $xxxController
     * @param $conname
     */
    static function addmenu($roures,$controller_path,$xxxController,$conname,$inputdata){
        $buttons = array_merge($inputdata['buttons_top'], $inputdata['buttons_rigth'], $inputdata['buttons_row']);
        $titles = [
            'add'=>'新增',
            'edit'=>'修改',
            'dele'=>'删除',
            'lists'=>'列表',
            'info'=>'详情',
            'pldele'=>'批量删除',
            'filter'=>'列筛选',
            'exports'=>'导出',
            'print'=>'打印',
        ];

        $m_buttons = [];
        foreach ($buttons as $button){
            $m_buttons[]=[
                "title"=>isset($titles[$button])?$titles[$button]:'未知'
                ,"rule"=>""
                ,"class"=>"LaravelRule-$button"
            ];
        }
        $m_buttons = json_encode($m_buttons,JSON_UNESCAPED_UNICODE);
        $pid=DB::table('admin_auth_rule')->insertGetId([
            'title'=>$conname?$conname:date("m-d H:i生成"),
            'ismenu'=>1,
            'status'=>1,
            'icon'=>'layui-icon-tabs'
        ]);
        $listrout = '';
        $makecontent = [];
        $xxxController = explode('Controller', $xxxController)[0];
        $xxxController = strtolower($xxxController);
        foreach ($roures as $roure){
            if(isset($titles[$roure])){
                $rule="/admin/{$xxxController}/{$roure}";
                if($roure=='lists') $listrout = $rule;
                $makecontent[]=[
                    'rule'=>$rule,
                    'title'=>$titles[$roure],
                    'ismenu'=>$roure=='lists'?1:0,
                    'pid'=>$pid,
                    'buttons'=>$roure=='lists'?$m_buttons:'{}',
                    'status'=>1,
                ];


            }

        }

        if(!empty($makecontent)){
            DB::table('admin_auth_rule')->insert($makecontent);
        }
        return $listrout;

    }




}
